﻿namespace Baddies.GUI
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Baddies.Events;  
    using Baddies.Scenes;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;      
    
    /// <summary>
    /// Box for text input.
    /// </summary>
    public class TextInputGUI : BoxGUI
    {
        /// <summary>
        /// Reference to the SpriteFont we are going to use.
        /// </summary>
        private SpriteFont spriteFont;

        /// <summary>
        /// Text to write.
        /// </summary>
        private string text;

        /// <summary>
        /// Color to apply to the text.
        /// </summary>
        private Color textColor;

        /// <summary>
        /// Sets the color of the text.
        /// </summary>
        /// <value>Color we want to apply to the text.</value>
        public Color TextColor
        {
            set { this.textColor = value; }
        }

        /// <summary>
        /// Initializes a new instance of the TextInputGUI class.
        /// </summary>
        public TextInputGUI()
        {
            this.spriteFont = Scene.Content.Load<SpriteFont>("Fonts/ConsoleFont");
            this.text = string.Empty;
            this.textColor = Color.White;

            EventManager.GetEV.RegisterNewEvent("ConsoleMsg", true);
        }

        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="spritebatch">Reference to the SpriteBatch to draw on.</param>
        public override void Draw(SpriteBatch spritebatch)
        {
            base.Draw(spritebatch);

            spritebatch.DrawString(
                this.spriteFont, 
                this.text, 
                new Vector2(this.DrawPosition.X, this.DrawPosition.Y), 
                this.textColor);
        }

        /// <summary>
        /// Uses the keys to moddify the string.
        /// </summary>
        /// <param name="key">Key to react to.</param>
        /// <returns>True if the key was used, false otherwise.</returns>
        public override bool React(Keys key)
        {
            int intKeyVal = (int)key;

            if (intKeyVal >= (int)Keys.A && intKeyVal <= (int)Keys.Z) 
            {
                // It's a relevant key, add it to text.
                this.text += key;
                return true;
            }
            else if (intKeyVal >= (int)Keys.D0 && intKeyVal <= (int)Keys.D9)
            {
                this.text += intKeyVal - (int)Keys.D0;
                return true;
            }
            else if (key == Keys.Back)
            {
                this.text = this.text.Remove(this.text.Length - 1);
                return true;
            }
            else if (key == Keys.Space)
            {
                this.text += " ";
                return true;
            }
            else if (key == Keys.Enter)
            {
                ConsoleMsgArg args = new ConsoleMsgArg();
                args.Text = this.text;
                EventManager.GetEV.Trigger("ConsoleMsg", this, args);
                this.text = string.Empty;
                return true;
            }

            return false;
        }
    }
}
